package practice.fourth.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import practice.fourth.feignRemoteService.FeignRemoteProvider;

/**
 * @author :Colin
 * @version 创建时间: 2021/9/11 10:19
 */
@RestController
@Slf4j
public class ConsumerController {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RestTemplate loadBalancedTemplate;
    @Autowired
    private FeignRemoteProvider feignRemoteProvider;

    @Value("${spring.application.name}")
    String appName;

    /*@GetMapping("/consumer/echo")
    public String doEcho(){

        String url="http://localhost:9100/provider/"+appName;
        log.info("url is ="+url);
        return restTemplate.getForObject(url,String.class);
    }
*/



    @GetMapping("/consumer/echo2")
    public String doEcho2(){
        String host = "sca-provider";
        String url=String.format("http://%s/provider/%s",host,appName);
        log.info("url is "+url);
        return loadBalancedTemplate.getForObject(url,String.class);
    }

    @GetMapping("/consumer/echo3")
    public String doEcho3(){

        return feignRemoteProvider.doGetMsg(appName);
    }

}
